import { request } from 'ice';
import { defaultType } from '@/services/user';

// 分页信息
export interface PagingInfo {
  // 页码
  skip?: number;
  // 数量
  take?: number;
}

// 返回页码
export interface Pages<T> {
  results: T;
  // 总数据量
  total: number;
  // 总页数
  totalPages: number;
  // 剩下页数
  remainingPages: number;
}

// 图片
export interface image {
  id: number;
  name: string;
  url: string;
  type: string;
  createdAt: string;
}


// 获取自己的图片列表
export const myImages = async (pagingInfo?: PagingInfo, name?: string) => {
  if (pagingInfo?.skip) {
    pagingInfo.skip = pagingInfo.skip - 1;
  }
  if (name == '') name = undefined;
  return await request<defaultType<Pages<image[]>>>({
    url: '/images/my-images',
    method: 'GET',
    params: {
      ...pagingInfo,
      name: name,
    },
    headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
  });
};

// 删除图片 根据图片ID
export const delById = async (id: number) => {
  return await request<defaultType<boolean>>({
    url: '/images/del',
    method: 'DELETE',
    data: { id },
    headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
  });
};

// 获取所有图片列表 管理员
export const allImages = async (pagingInfo?: PagingInfo, name?: string) => {
  if (pagingInfo?.skip) {
    pagingInfo.skip = pagingInfo.skip - 1;
  }
  if (name == '') name = undefined;
  return await request<defaultType<Pages<image[]>>>({
    url: '/images/all-images',
    method: 'GET',
    params: {
      ...pagingInfo,
      name: name,
    },
    headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
  });
};
